<?php
/**
 * 患者信息管理控制器
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/22
 * Time: 22:00
 */

namespace Admin\Controller;


use Base\Controller\CurdController;
use Library\UploadFile;

class PatientInfoController extends CurdController
{

    public function __construct()
    {
        parent::__construct(true, true, array(
            'input_name' => 'coverFile',//文本框name
            'save_path' => 'patienthead',//保存路径
            'data_field' => 'cover'//数据库字段
        ));
    }
    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['phone'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['realname']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
            $where['status']=array('egt',0);
        }else{
            $where['status']=-1;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);
//        dump($data['list']);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }

    /**
     * 新增患者信息
     */
    public function add()
    {
        $data = $this->model->create();
        if (!$data) {
            $this->error($this->model->getError());
        } else {
            if ($this->upload) {
                //上传图片
                $input_name = $this->uploadConfig['input_name'];
                $save_path = $this->uploadConfig['save_path'];
                $data_field = $this->uploadConfig['data_field'];
                if ($_FILES[$input_name]) {
                    $up = new UploadFile();
                    $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                    $data[$data_field] = $info[$input_name]['savepath'] . $info[$input_name]['savename'];
                }
            }
            $data['passwd']=md5(C('md5s').$data['passwd']);
            $englishname=$this->piny($data['realname']);
            $data['en_realname']=$englishname;
            $data = $this->addAfter($data);
            $add = $this->model->add($data);
            $datas['id']=$add;
            $datas['name']=C('Patient').$add;
            $res=$this->model->save($datas);
            $this->addEnd($res);
        }
    }

    /**
     * 编辑患者信息
     */
    public function edit()
    {
        $data = $this->model->create();
        if (!$data) {
            $this->error($this->model->getError());
        } else {
            //检查密码
            $this->checkPwd();
            if ($this->upload) {
                $input_name = $this->uploadConfig['input_name'];
                $save_path = $this->uploadConfig['save_path'];
                $data_field = $this->uploadConfig['data_field'];
                if ($_FILES[$input_name]) {
                    $sel = $this->model->find($data['id']);
                    if ($sel[$data_field]) {
                        //删除
                        $upConfig = C('UPLOADS');
                        removeFile($upConfig['PATH'] . '/' . $sel[$data_field]);
                    }
                    $up = new UploadFile();
                    $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                    $data[$data_field] = $info[$input_name]['savepath'] . $info[$input_name]['savename'];
                }
            }
            if(empty($data['birthday'])){
               $data['birthday']=date("Y-m-d H:i:s", time());
            }
            $add = $this->model->save($data);
            if ($add) {
                $this->success('操作成功');
            }
        }
    }

    /**
     * @param $id
     * 显示患者信息
     */
    public function showInfo($id){
        //查询患者信息
        $data = $this->model->getOne($id);
        $this->assign('data',$data);
        $this->display('Doctor_showInfo');
    }

    /**
     * @param string $id
     * 患者删除
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $this->model->where('id=' . $v)->setField('status',-1);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }
    /**
     * 恢复删除
     * @param string $id 要恢复的id，多个以,分割
     */
    public function recover($id){
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $this->model->where('id=' . $v)->setField('status',1);
            $i++;
        }
        $this->success('恢复' . $i . '条数据', $this->urlConfig['del_success']);
    }
}